package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.AdminParam;
import com.cskaoyan.bean.pojo.MallAdmin;
import com.cskaoyan.bean.pojo.MallAdminExample;
import com.cskaoyan.mapper.MallAdminMapper;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.util.MyPageHelper;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Fengyi
 * @description: adminservice 实现类
 * @time: 2021/7/8 20:54
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MallAdminMapper mallAdminMapper;

    /**
     * @Author fengyi
     * @description: 分页查询adminlist
     * @Date 13:12 2021/7/9
     * url:
     */
    @Override
    public BaseData<MallAdmin> queryAdmin(AdminParam param) {

//        PageHelper.startPage(param.getPage(),param.getLimit());
        MyPageHelper.startPage(param);


        MallAdminExample mallAdminExample = new MallAdminExample();
        MallAdminExample.Criteria criteria = mallAdminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (param.getUsername() != null) {
            criteria.andUsernameLike("%" + param.getUsername() + "%");
        }

        mallAdminExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallAdmin> admins = mallAdminMapper.selectByExample(mallAdminExample);
        for (MallAdmin admin : admins) {
            admin.setPassword("");
        }


        long total = mallAdminMapper.countByExample(mallAdminExample);

        return BaseData.ok(admins, total);
    }



    /**
     * @Author fengyi
     * @description: 添加用户
     * @Date 13:12 2021/7/9
     * url:
     */
    @Override
    public int addAdmin(MallAdmin mallAdmin) {
//        mallAdminMapper.insert();
        MallAdminExample mallAdminExample = new MallAdminExample();

        MallAdminExample.Criteria criteria = mallAdminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andUsernameEqualTo(mallAdmin.getUsername());
        List<MallAdmin> mallAdmins = mallAdminMapper.selectByExample(mallAdminExample);
        if(mallAdmins.size() != 0) {
            //有重名
            return -1;
        }
        mallAdminMapper.insertSelective(mallAdmin);
        return mallAdmin.getId();
    }

    @Override
    public void deleteAdmin(MallAdmin mallAdmin) {
        mallAdminMapper.deleteLogicalById(mallAdmin.getId(), new Date());
    }

    @Override
    public int updateService(MallAdmin mallAdmin) {
        MallAdminExample mallAdminExample = new MallAdminExample();
        MallAdminExample.Criteria criteria = mallAdminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        String username = mallAdminMapper.selectOldNameById(mallAdmin.getId());

        criteria.andUsernameNotEqualTo(username);
        criteria.andUsernameEqualTo(mallAdmin.getUsername());
        List<MallAdmin> mallAdmins = mallAdminMapper.selectByExample(mallAdminExample);
        if(mallAdmins.size() != 0) {
            return 404;
        }

        mallAdminMapper.updateByPrimaryKeySelective(mallAdmin);
        return 200;
    }


}
